package com.ciban.smarthome.module.ui;

import java.util.ArrayList;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ciban.smarthome.R;
import com.ciban.smarthome.common.utils.ImgUtils;
import com.ciban.smarthome.common.utils.StringUtils;
import com.ciban.smarthome.module.ui.base.BaseActivity;
import com.ciban.smarthome.module.db.dao.AreaDao;
import com.ciban.smarthome.module.db.dao.HostDao;
import com.ciban.smarthome.module.engine.AreaEngine;
import com.ciban.smarthome.module.entity.Area;
import com.ciban.smarthome.module.entity.Host;

public class AreaModifyActivity extends BaseActivity implements OnClickListener {
    private static final int USE_CAMERA = 100;
    private static final int USE_IMGS = 101;
    private ImageView iv_camera, iv_imgs, iv_img;
    private LinearLayout ll_host;
    private TextView tv_host;
    private Button bt_save;
    private EditText et_name;

    private Area area;
    private boolean isAdd = true;
    private Uri photoUri;
    private ArrayList<Host> mainEngList;
    private int show_mode;

    private AreaEngine engine;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_area_modify);
        initData();
        initViews();
    }

    private void initData() {
        if (getIntent().hasExtra("area")) {
            isAdd = false;
            area = (Area) getIntent().getExtras().get("area");
        } else {
            area = new Area();
        }

        engine = new AreaEngine(this);
    }

    private void initViews() {
        String title = "添加区域";
        if (!isAdd)
            title = "修改-" + area.getName();
        ((TextView) findViewById(R.id.title)).setText(title);
        ((ImageView) findViewById(R.id.title_add)).setVisibility(View.GONE);
        ((ImageView) findViewById(R.id.title_back)).setOnClickListener(this);

        iv_camera = (ImageView) findViewById(R.id.area_add_camera);
        iv_imgs = (ImageView) findViewById(R.id.area_add_imgs);
        iv_img = (ImageView) findViewById(R.id.area_add_img);

        ll_host = (LinearLayout) findViewById(R.id.area_add_ll_host);
        tv_host = (TextView) findViewById(R.id.area_add_tv_host);

        bt_save = (Button) findViewById(R.id.area_add_bt_save);

        et_name = (EditText) findViewById(R.id.area_add_et_name);

        if (!isAdd)
            initDataView();

        setListeners();

    }

    private void initDataView() {

        displayImg(area.getIcon_path(), iv_img);

        if (!StringUtils.isEmpty(area.getHost_id())) {
            HostDao dao = new HostDao(this);
            Host hostDetail = dao.getMainEngineDetail(area.getHost_id());
            String mainName = hostDetail.getName();
            tv_host.setText(mainName);
            dao = null;
        }

        et_name.setText(area.getName());
    }

    private void setListeners() {
        iv_camera.setOnClickListener(this);
        iv_imgs.setOnClickListener(this);
        ll_host.setOnClickListener(this);
        bt_save.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case USE_CAMERA:
                if (resultCode == RESULT_OK)
                    iv_img.setImageURI(photoUri);
                break;
            case USE_IMGS:
                if (resultCode == RESULT_OK) {
                    photoUri = data.getData();
                    iv_img.setImageURI(photoUri);
                }
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.area_add_camera:
                photoUri = ImgUtils.useCamera(this, USE_CAMERA);
                break;
            case R.id.area_add_imgs:
                ImgUtils.useImgs(this, USE_IMGS);
                break;
            case R.id.area_add_ll_host:
                chooseHost();
                break;
            case R.id.area_add_bt_save:
                saveArea();
                break;
        }
    }

    private void saveArea() {
        String name = et_name.getText().toString().trim();
        if (StringUtils.isEmpty(name)) {
            displayShort("请输入名称");
            return;
        }

        String path = area.getIcon_path();
        if (photoUri != null)
            path = photoUri.toString();

        area.setName(name);
        area.setIcon_path(path);

        boolean isSuccess = false;
        AreaDao dao = new AreaDao(this);

        if (isAdd) {
            long id = dao.addNewArea(area);
            if (id >= 0)
                isSuccess = true;
        } else {
            int num = dao.updateArea(area);
            if (num > 0)
                isSuccess = true;
        }
        if (!isSuccess)
            return;

        displayShort("操作成功");
        Intent intent = new Intent();
        intent.putExtra("area", area);
        setResult(RESULT_OK, intent);
        finish();
    }

    private void chooseHost() {
        getHostList();

        ArrayList<String> showList = new ArrayList<String>();
        for (Host info : mainEngList)
            showList.add(info.getName());

        if (showList.size() < 1) {
            displayShort("请先添加主机");
            return;
        }
        showLvDialog(null, "选择主机", showList, true);
        show_mode = 0;
    }

    @Override
    protected void lvItemClick(int position) {
        super.lvItemClick(position);
        if (show_mode == 0) {
            Host info = mainEngList.get(position);
            tv_host.setText(info.getName());
            area.setHost_id(info.getId());
        }
    }

    private void getHostList() {
        if (mainEngList == null) {
            HostDao dao = new HostDao(this);
            mainEngList = dao.getMainEngList();
            dao = null;
        }
    }

}
